from django.contrib import admin
from apps.course.models import Course, Lesson, Video, BannerCourse, CourseResource, CourseTag
from import_export import resources
from import_export.admin import ImportExportModelAdmin


# Register your models here.


class LessonInline(admin.TabularInline):
    model = Lesson
    style = "tab"
    extra = 0
    exclude = ["add_time"]


class CourseResourceInline(admin.TabularInline):
    model = CourseResource
    style = "tab"
    extra = 1


@admin.register(Course)
# class CourseAdmin(admin.ModelAdmin):
class CourseAdmin(ImportExportModelAdmin):
    list_display = ['name', 'desc', 'detail', 'degree', 'learn_times', 'students', 'show_image', 'go_to']
    search_fields = ['name', 'desc', 'detail', 'degree', 'students']
    list_filter = ['name', 'teacher__name', 'desc', 'detail', 'degree', 'learn_times', 'students']
    list_editable = ["degree", "desc"]
    model_icon = 'fa fa-address-book'
    inlines = [LessonInline, CourseResourceInline]

    def queryset(self):
        qs = super().queryset()
        if not self.request.user.is_superuser:
            qs = qs.filter(teacher=self.request.user.teacher)
        return qs


@admin.register(CourseTag)
class CourseTagAdmin(admin.ModelAdmin):
    list_display = ['course', 'tag', 'add_time']
    search_fields = ['course', 'tag']
    list_filter = ['course', 'tag', 'add_time']


@admin.register(BannerCourse)
class BannerCourseAdmin(admin.ModelAdmin):
    list_display = ['name', 'desc', 'detail', 'degree', 'learn_times', 'students']
    search_fields = ['name', 'desc', 'detail', 'degree', 'students']
    list_filter = ['name', 'teacher__name', 'desc', 'detail', 'degree', 'learn_times', 'students']
    list_editable = ["degree", "desc"]

    def queryset(self):
        qs = super().queryset()
        qs = qs.filter(is_banner=True)
        return qs


@admin.register(Lesson)
class LessonAdmin(admin.ModelAdmin):
    list_display = ['course', 'name', 'add_time']
    search_fields = ['course', 'name']
    list_filter = ['course__name', 'name', 'add_time']


@admin.register(Video)
class VideoAdmin(admin.ModelAdmin):
    list_display = ['lesson', 'name', 'add_time']
    search_fields = ['lesson', 'name']
    list_filter = ['lesson', 'name', 'add_time']


@admin.register(CourseResource)
class CourseResourceAdmin(admin.ModelAdmin):
    list_display = ['course', 'name', 'file', 'add_time']
    search_fields = ['course', 'name', 'file']
    list_filter = ['course', 'name', 'file', 'add_time']


class CourseResource(resources.ModelResource):
    class Meta:
        model = Course
